package cn.yunhe.service.implService;

import cn.yunhe.dao.ItopicDao;
import cn.yunhe.entity.Topic;
import cn.yunhe.service.ItopicService;
import cn.yunhe.util.Paganation;
import cn.yunhe.util.SqlSessionFactoryUtil;
import org.apache.ibatis.session.SqlSession;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class TopicService implements ItopicService {
    private SqlSession session=SqlSessionFactoryUtil.getSqlSession();
    private ItopicDao tdao=session.getMapper(ItopicDao.class);

    @Override
    public Topic getTopicByTid(int tid) {
        Topic topic=new Topic();
        topic=tdao.getTopicByTid(tid);
        session.close();
        return topic;
    }

    @Override
    public Paganation<Topic> getAllTopic(Topic topic, Paganation paganation) {
        Paganation<Topic> paganation1=new Paganation<Topic>();
        Map<String ,Object> map=new HashMap<String ,Object>();//为查询所有主贴内容创建map
        map.put("start",Paganation.getPageSize()*(paganation.getPageNo()-1));
        map.put("ps",Paganation.getPageSize());
        map.put("mypaganation",paganation);
        map.put("mytopic",topic);
        List<Topic> topicList=tdao.getAllTopic(map);
        Map<String ,Object> map1=new HashMap<String, Object>();//为查询和topic相对应条件的主贴总记录数
        map1.put("mypaganation",paganation);
        map1.put("mytopic",topic);
        int totalRows=tdao.getCountTopic(map1);
        paganation1.setPageNo(paganation.getPageNo());
        paganation1.setTotalRows(totalRows);
        paganation1.setItems(topicList);
        session.close();
        return paganation1;
    }

    @Override
    public boolean addTopic(Topic topic) {
        boolean flag=tdao.addTopic(topic);
        session.commit();
        session.close();
        return flag;
    }

    @Override
    public boolean updateTopic(Topic topic) {
        boolean flag=tdao.updateTopic(topic);
        session.commit();
        session.close();
        return flag;
    }

    @Override
    public boolean deleteTopic(Topic topic) {
        boolean flag=tdao.deleteTopic(topic);
        session.commit();
        session.close();
        return flag;
    }
}
